import {Store, Getter, Mutation, Action} from 'decorators/vuex'
import { contains } from '@/utils/commonMethod/method.js'
@Store
export class SettingStore {
  productAddType = '0'
  productBySelf = {}
  productByForm = {}
  productEdit={}
  // 存model集合
  storeProductSelectFilter = {}

  // 存value集合
  storeProductSelectValue = {}

  storeProductCurrentSuperId = ''

  storeProductCurrentSelects = []

  storeProductBrandLetters = {}

  // 求买编辑信息保存
  address = {}
  wantBuyAddProductCover = ''
  wantBuyAddProductDesc= ''
  wantBuyAddPriceType = ''
  wantBuyAddPrice = ''

  @Getter
  wantBuyAddProductDesc (state) {
    return state.wantBuyAddProductDesc
  }

  @Getter
  wantBuyAddPriceType (state) {
    return state.wantBuyAddPriceType
  }
   @Getter
  wantBuyAddPrice (state) {
    return state.wantBuyAddPrice
  }

  @Getter
  address (state) {
    return state.address
  }

  @Getter
  wantBuyAddProductCover (state) {
    return state.wantBuyAddProductCover
  }

  @Getter
  productAddType (state) {
    return state.productAddType
  }

  @Getter
  productBySelf (state) {
    return state.productBySelf
  }

  @Getter
  productEdit (state) {
    return state.productEdit
  }

  @Getter
  productByForm (state) {
    return state.productByForm
  }

  @Getter
  storeProductBrandLetters(state) {
    return state.storeProductBrandLetters
  }

  @Getter
  storeProductSelectFitler(state) {
    return state.storeProductSelectFilter
  }

  @Getter
  storeProductSelectValue (state) {
    return state.storeProductSelectValue
  }

  @Getter
  storeProductCurrentSelects(state) {
    return state.storeProductCurrentSelects
  }

  @Getter
  storeProductCurrentSuperId(state) {
    return state.storeProductCurrentSuperId
  }

  @Mutation
  setWantBuyAddPrice (state, wantBuyAddPrice) {
    state.wantBuyAddPrice = wantBuyAddPrice
  }
  @Mutation
  setWantBuyAddPriceType (state, wantBuyAddPriceType) {
    state.wantBuyAddPriceType = wantBuyAddPriceType
  }

  @Mutation
  setWantBuyAddProductCover (state, wantBuyAddProductCover) {
    state.wantBuyAddProductCover = wantBuyAddProductCover
  }
  @Mutation
  setAddress (state, address) {
    state.address = address
  }

  @Mutation
  setProductAddType (state, type) {
    state.productAddType = type
  }

  @Mutation
  setProductBySelf (state, product) {
    state.productBySelf = product
  }

   @Mutation
  setProductEdit (state, product) {
    state.productEdit = product
  }

  @Mutation
  setProductByForm (state, product) {
    state.productByForm = product
  }

  @Mutation
  setStoreProductSelectValue (state, selectValue) {
    state.storeProductSelectValue = selectValue
  }

  @Mutation
  setStoreProductBrandLetters (state, letters) {
    state.storeProductBrandLetters = letters
  }

  @Mutation
  setStoreProductCurrentSelects (state, selectItems) {
    state.storeProductCurrentSelects = selectItems
  }

  @Mutation
  setStoreProductSelectFilter (state, selectFilter) {
    state.storeProductSelectFilter = selectFilter
  }

  @Mutation
  setStoreProductCurrentSuperId (state, superId) {
    let arr = state.storeProductSelectFilter[superId]
    if (!arr) {
      state.storeProductSelectFilter[superId] = []
      state.storeProductSelectValue[superId] = []
    }

    state.storeProductCurrentSuperId = superId
  }

  @Action clearStoreProductSelect ({commit}) {
    commit('setStoreProductSelectFilter', [])
    commit('setStoreProductCurrentSuperId', '')
    commit('setStoreProductSelectValue', {})
    commit('setStoreProductCurrentSuperId', '')
  }

  @Action
  changeStoreProductCurrentSuperId ({commit}, superId) {
    commit('setStoreProductCurrentSuperId', superId)
  }

  @Action selectStoreProductFilterItem ({state}, item) {
    // 通过superId 找到对应的已选中数组
    const superIdSelectArr = state.storeProductSelectFilter[state.storeProductCurrentSuperId]
    const superIdSelectValue = state.storeProductSelectValue[state.storeProductCurrentSuperId]

    // 判断选中的是否已存在
    if (contains(superIdSelectArr, item)) {
      for (let i in superIdSelectArr) {
        if (superIdSelectArr[i] === item) {
          superIdSelectArr.splice(i, 1)
          superIdSelectValue.splice(i, 1)
          state.storeProductSelectFilter[state.storeProductCurrentSuperId] = superIdSelectArr
          state.storeProductSelectValue[state.storeProductCurrentSuperId] = superIdSelectValue
          return
        }
      }
    } else {
      superIdSelectArr.push(item)
      if (item && item.value) {
        superIdSelectValue.push(item.value)
      }
      state.storeProductSelectValue[state.storeProductCurrentSuperId] = superIdSelectValue
      state.storeProductSelectFilter[state.storeProductCurrentSuperId] = superIdSelectArr
    }
  }

  @Action getStoreProductSelectBySuperId({commit, state}, superId) {
    let arr = state.storeProductSelectFilter[superId]
    commit('setStoreProductCurrentSelects', arr)
  }
}
